Ext.onReady(function(){ 

Ext.QuickTips.init();

var fm = Ext.form;

var sm = new Ext.grid.CheckboxSelectionModel({  
        dataIndex:"userId",  
        listeners:{'beforerowselect': function( SelectionModel, rowIndex, keepExisting,record ) {  
                  
                       return true; //不能进行选择  
                   
        }}  
});

//Gridpanel
var cm=new Ext.grid.ColumnModel([
sm,
{header:'流水号',dataIndex:'serialNo',hidden:true,
editor: new fm.TextField({allowBlank: false,editable:false,readOnly:true})
} ,
{header:'大类因素',
	sortable: true, renderer: function (value, meta, record, rowIndex, colIndex, store) {
                    var first = !rowIndex || value !== store.getAt(rowIndex - 1).get('describe1'),
                        last = rowIndex >= store.getCount() - 1 || value !== store.getAt(rowIndex + 1).get('describe1');
                    meta.css += 'row-span' + (first ? ' row-span-first' : '') +  (last ? ' row-span-last' : '');
                    if (first) {
                        var i = rowIndex + 1;
                        while (i < store.getCount() && value === store.getAt(i).get('describe1')) {
                            i++;
                        }
                        var rowHeight = 20, padding = 6,
                            height = (rowHeight * (i - rowIndex) - padding) + 'px';
                        meta.attr = 'style="height:' + height + ';line-height:' + height + ';"';
                    }
                    return first ? value : '';
                }, 
	dataIndex:'describe1',
	width:100,
	editor: new fm.TextField({
                allowBlank: false
            })
    },
{header:'子类因素',
			sortable: true, renderer: function (value, meta, record, rowIndex, colIndex, store) {
                    var first = !rowIndex || value !== store.getAt(rowIndex - 1).get('describe2'),
                        last = rowIndex >= store.getCount() - 1 || value !== store.getAt(rowIndex + 1).get('describe2');
                    meta.css += 'row-span' + (first ? ' row-span-first' : '') +  (last ? ' row-span-last' : '');
                    if (first) {
                        var i = rowIndex + 1;
                        while (i < store.getCount() && value === store.getAt(i).get('describe2')) {
                            i++;
                        }
                        var rowHeight = 20, padding = 6,
                            height = (rowHeight * (i - rowIndex) - padding) + 'px';
                        meta.attr = 'style="height:' + height + ';line-height:' + height + ';"';
                    }
                    return first ? value : '';
                }, 
		dataIndex:'describe2',
		width:100,
		editor: new fm.TextField({
                allowBlank: false
            })	
},
{header:'因素说明',
	sortable: true, renderer: function (value, meta, record, rowIndex, colIndex, store) {
                    var first = !rowIndex || value !== store.getAt(rowIndex - 1).get('describe3'),
                        last = rowIndex >= store.getCount() - 1 || value !== store.getAt(rowIndex + 1).get('describe3');
                    meta.css += 'row-span' + (first ? ' row-span-first' : '') +  (last ? ' row-span-last' : '');
                    if (first) {
                        var i = rowIndex + 1;
                        while (i < store.getCount() && value === store.getAt(i).get('describe3')) {
                            i++;
                        }
                        var rowHeight = 20, padding = 6,
                            height = (rowHeight * (i - rowIndex) - padding) + 'px';
                        meta.attr = 'style="height:' + height + ';line-height:' + height + ';"';
                    }
                    return first ? value : '';
                },
dataIndex:'describe3',
width:100,
editor: new fm.TextField({
                allowBlank: false
            })
}, 
{header:'描述',
dataIndex:'itemDescribe',
width:100,
editor: new fm.TextField({
                allowBlank: false
})
},
{header:'选项得分',
dataIndex:'itemValue',
width:40,
editor: new fm.NumberField({
                allowBlank: false,
                allowNegative: false
            })
},
{header:'绝对权重(%)',
	sortable: true, renderer: function (value, meta, record, rowIndex, colIndex, store) {
                    var first = !rowIndex || value !== store.getAt(rowIndex - 1).get('itemWeight'),
                        last = rowIndex >= store.getCount() - 1 || value !== store.getAt(rowIndex + 1).get('itemWeight');
                    meta.css += 'row-span' + (first ? ' row-span-first' : '') +  (last ? ' row-span-last' : '');
                    if (first) {
                        var i = rowIndex + 1;
                        while (i < store.getCount() && value === store.getAt(i).get('itemWeight')) {
                            i++;
                        }
                        var rowHeight = 20, padding = 6,
                            height = (rowHeight * (i - rowIndex) - padding) + 'px';
                        meta.attr = 'style="height:' + height + ';line-height:' + height + ';"';
                    }
                    return first ? value : '';
                },
dataIndex:'itemWeight',
width:40,
editor: new fm.NumberField({
                allowBlank: false,
                allowNegative: false
            })
}
]); 

var ds = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'model/listModelItems.do?dataFlag=2',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ 
totalProperty:'total',  
root:'resultData'},[
{name:'serialNo'}, 
{name:'describe1'}, 
{name:'describe2'},  
{name:'describe3'},
{name:'itemDescribe'},
{name:'itemValue'},
{name:'itemWeight',type: 'float',convert:function formatFloat(val){return Number(Number(val).toFixed(3));}}
]) 
}); 
ds.load(); 


var grid=new Ext.grid.EditorGridPanel({ 
id:'myCustomers',
renderTo:"panel",
stripeRows:true,//斑马线效果
title:'<div align="center">已添加的定性因素</div>', 
height:500, 
cm:cm,
store: ds, 
clicksToEdit: 2,
frame: true,
columnLines:true, 
cls: 'grid-row-span',
loadMask: { msg: "加载中..." }, 
viewConfig: {    forceFit:true   } ,
/*   tbar:[{  
    text:'提交修改',  
    handler:function(){  
     var mr=ds.getModifiedRecords();//获取所有更新过的记录  
     alert(mr[0].data["describe1"]);
     var recordCount=ds.getCount();//获取数据集中记录的数量  
     if(mr.length==0){  // 确认修改记录数量  
       alert("没有修改数据！");  
       return false;  
     }   
     var recordModStr="[";//这里以josn方式保存  
     for(var i=0;i<mr.length;i++){  
     
      recordModStr+="{describe1:"+mr[i].data["describe1"]+",describe2:"+mr[i].data["describe2"]+",describe3:"  
           +mr[i].data["describe3"]+",itemDescribe:"+mr[i].data["itemDescribe"]+",itemValue:"+mr[i].data["itemValue"]+",itemWeight:"+mr[i].data["itemWeight"]+"}";  
       if(i<mr.length-1)  
                recordModStr+=",";  
     }  
     recordModStr+="]";  
     alert(recordModStr);  
     //向后台提交请求 Ext.Ajax.request(requestCofig);//将recordModStr传入  
   }  
   }] */
                      
});

function isEdit(value,record){  
    //向后台提交请求  
    alert(record.data.serialNo);
   return value;  
}  
function afterEdit(obj){    //每次更改后，触发一次该事件  
	var row = obj.record;
	var col = obj.field;
	alert(row.data.serialNo);
	alert(col);
   alert("orginalValue:"+obj.originalValue+",value:"+obj.value);  
   
	Ext.Ajax.request({
		url:'model/editQua.do',
		params : { serialNo: row.data.serialNo,
			field:col, 
			originalValue:obj.originalValue, 
			modifiedValue:obj.value
		},
		method:'POST',
		success:function(){
			Ext.MessageBox.alert("提示","修改成功！");
			grid.store.reload(); 
		},failure:function(){
			Ext.MessageBox.alert("提示", "修改失败！"); 
		}
	});
 
}  
grid.on("afteredit", afterEdit, grid);  

}); 